<?php
namespace Orm\OrderDetail;

use Base\Exception\RmException;
class OrderDetailClass extends \Base\BaseOrmObject{
    protected   $id,
				$order_id = 0, // 关联订单ID
				$product_id = 0, // 产品ID
				$unit_price = 0, // 商品单价
				$amount = 0, // 购买数量
				$total_price = 0, // 总价
				$is_del = 0, // 默认0  是否删除 1是 0否
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }
    public function loadById($id = 0)
    {
        $model = new \Orm\OrderDetail\OrderDetailModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'order_id' => $this->order_id,
			'product_id' => $this->product_id,
			'unit_price' => $this->unit_price,
			'amount' => $this->amount,
			'total_price' => $this->total_price,
        );

        $model = new \Orm\OrderDetail\OrderDetailModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'order_id' => $this->order_id,
			'product_id' => $this->product_id,
			'unit_price' => $this->unit_price,
			'amount' => $this->amount,
			'total_price' => $this->total_price,
        );

        $model = new \Orm\OrderDetail\OrderDetailModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\OrderDetail\OrderDetailModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}